package cn.com.inossem.activity;

import android.content.Intent;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import cn.com.inossem.base.RequestActivity;
import cn.com.inossem.requestmanager.WooRequestFactory;
import cn.com.inossem.utils.ActivityStackControlUtil;

import com.foxykeep.datadroid.requestmanager.Request;

/**
 * 
 * 注册，修改密码
 * 
 * <p>
 * detailed comment
 * 
 * @author szc 2015-8-24
 * @see
 * @since 1.0
 */
public class RegisteredActivity extends RequestActivity {

	int type = 0;

	TextView title, submit, login, huoqu;

	EditText mima, querenmima, yanzhengma, username, truename, email, promocode;

	ImageView back;
	private MyCount mc;

	CheckBox checkbox;
	TextView xieyishu;

	LinearLayout checklayout;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// setContentView(R.layout.main_tab_layout);
		initView();

	}

	@Override
	public void onDestroy() {
		ActivityStackControlUtil.remove(this);
		super.onDestroy();
	}

	private void initView() {

		checkbox = (CheckBox) this.findViewById(R.id.checkbox);
		xieyishu = (TextView) this.findViewById(R.id.xieyishu);
		xieyishu.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				Uri uri = Uri.parse("http://www.inossem.com/agreement.html");
				Intent intent = new Intent(Intent.ACTION_VIEW, uri);
				startActivity(intent);
			}
		});

		back = (ImageView) this.findViewById(R.id.back);
		back.setOnClickListener(this);

		type = getIntent().getIntExtra("type", 0);
		title = (TextView) this.findViewById(R.id.title);

		mima = (EditText) this.findViewById(R.id.mima);
		querenmima = (EditText) this.findViewById(R.id.querenmima);
		yanzhengma = (EditText) this.findViewById(R.id.yanzhengma);
		username = (EditText) this.findViewById(R.id.username);
		promocode = (EditText) this.findViewById(R.id.promocode);

		truename = (EditText) this.findViewById(R.id.truename);
		email = (EditText) this.findViewById(R.id.email);
		huoqu = (TextView) this.findViewById(R.id.huoqu);
		huoqu.setOnClickListener(this);
		submit = (TextView) this.findViewById(R.id.submit);
		submit.setOnClickListener(this);
		login = (TextView) this.findViewById(R.id.login);
		login.setOnClickListener(this);

		checklayout = (LinearLayout) this.findViewById(R.id.checklayout);

		if (type == 1) {
			title.setText("修改密码");
			submit.setText("确认修改密码");
			login.setVisibility(View.GONE);
			checklayout.setVisibility(View.GONE);
			mima.setHint("修改密码");
			querenmima.setHint("确认修改密码");

			truename.setVisibility(View.GONE);
			email.setVisibility(View.GONE);
			promocode.setVisibility(View.GONE);
		}
	}

	@Override
	public Request getInitialRequest() {
		return null;

	}

	@Override
	public void onRequestSucess(Request request, Bundle bundle) {
		if (bundle.containsKey(WooRequestFactory.BUNDLE_EXTRA_WANGJIMIMA)) {

			int code = bundle.getInt(WooRequestFactory.BUNDLE_EXTRA_WANGJIMIMA);

			if (code == 1) {
				Toast.makeText(this, "修改成功!", Toast.LENGTH_LONG).show();
				finish();
			} else if (code == 5) {
				huoqu.setEnabled(true);
				Toast.makeText(this, "验证码错误!", Toast.LENGTH_LONG).show();
			} else if (code == 6) {
				huoqu.setEnabled(true);
				Toast.makeText(this, "用户名密码错误！", Toast.LENGTH_LONG).show();
			} else if (bundle.getInt(WooRequestFactory.BUNDLE_EXTRA_LOGIN) == 9) {
				huoqu.setEnabled(true);
				Toast.makeText(this, "用户不存在！", Toast.LENGTH_LONG).show();
			}

		} else if (bundle
				.containsKey(WooRequestFactory.BUNDLE_EXTRA_REGISTERED)) {
			int code = bundle.getInt(WooRequestFactory.BUNDLE_EXTRA_REGISTERED);
			if (code == 1) {
				setResult(999);
				finish();
			} else if (code == 3) {
				Toast.makeText(this, "用户名格式错误！", Toast.LENGTH_LONG).show();
			} else if (code == 4) {
				huoqu.setEnabled(true);
				Toast.makeText(this, "用户已存在!", Toast.LENGTH_LONG).show();
			} else if (code == 5) {
				huoqu.setEnabled(true);
				Toast.makeText(this, "验证码错误!", Toast.LENGTH_LONG).show();
			} else if (code == 6) {
				huoqu.setEnabled(true);
				Toast.makeText(this, "用户名密码错误！", Toast.LENGTH_LONG).show();
			} else if (code == 13) {
				huoqu.setEnabled(true);
				Toast.makeText(this, "该邀请人不存在！", Toast.LENGTH_LONG).show();
			} else if (bundle.getInt(WooRequestFactory.BUNDLE_EXTRA_LOGIN) == 9) {
				huoqu.setEnabled(true);
				Toast.makeText(this, "用户不存在！", Toast.LENGTH_LONG).show();
			}
		} else if (bundle
				.containsKey(WooRequestFactory.BUNDLE_EXTRA_UPDATESUCCESS)) {
			int errorFlag = bundle
					.getInt(WooRequestFactory.BUNDLE_EXTRA_UPDATESUCCESS);
			if (errorFlag == 1) {
				huoqu.setBackgroundResource(R.drawable.yanzhengmahui);
				mc = new MyCount(60000, 1000);
				mc.start();
			} else if (errorFlag == 4) {
				huoqu.setEnabled(true);
				Toast.makeText(this, "用户名已经存在！", Toast.LENGTH_LONG).show();
			} else if (errorFlag == 3) {
				huoqu.setEnabled(true);
				Toast.makeText(this, "用户名格式错误！", Toast.LENGTH_LONG).show();
			} else if (errorFlag == 5) {
				huoqu.setEnabled(true);
				Toast.makeText(this, "验证码错误！", Toast.LENGTH_LONG).show();
			} else if (errorFlag == 6) {
				huoqu.setEnabled(true);
				Toast.makeText(this, "用户名密码错误！", Toast.LENGTH_LONG).show();
			} else if (errorFlag == 7) {
				huoqu.setEnabled(true);
				Toast.makeText(this, "短信验证码获取错误！", Toast.LENGTH_LONG).show();
			} else if (errorFlag == 9) {
				huoqu.setEnabled(true);
				Toast.makeText(this, "用户不存在！", Toast.LENGTH_LONG).show();
			}
		}
	}

	@Override
	public void onRequestError(int exceptionType) {

	}

	public int getContentViewId() {
		return R.layout.registered_layout;

	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.huoqu:
			if (type == 1)
				launchRequest(WooRequestFactory.Yanzhengma("phone", username
						.getText().toString(), "4"));
			else
				launchRequest(WooRequestFactory.Yanzhengma("phone", username
						.getText().toString(), "1"));

			huoqu.setEnabled(false);
			break;
		case R.id.back:
			finish();
			break;
		case R.id.login:
			finish();
			break;
		case R.id.submit:
			if (!checkbox.isChecked() && type != 1) {
				Toast.makeText(this, "请勾选同意用户使用协议", Toast.LENGTH_LONG).show();
				return;
			}
			if (type == 1) {
				if (username.getText().toString().equals("")
						|| mima.getText().toString().equals("")
						|| yanzhengma.getText().toString().equals("")) {
					Toast.makeText(this, "全部内容不能为空!", Toast.LENGTH_LONG).show();
					return;
				}
			} else {
				if (username.getText().toString().equals("")
						|| mima.getText().toString().equals("")
						|| yanzhengma.getText().toString().equals("")
						|| truename.getText().toString().equals("")
						|| email.getText().toString().equals("")) {
					Toast.makeText(this, "全部内容不能为空!", Toast.LENGTH_LONG).show();
					return;
				}
			}

			if (mima.getText().toString()
					.equals(querenmima.getText().toString())) {
				if (type == 0) {// 注册
					launchRequest(WooRequestFactory.getRegistered(username
							.getText().toString(), mima.getText().toString(),
							truename.getText().toString(), email.getText()
									.toString(), yanzhengma.getText()
									.toString(), promocode.getText().toString()));
				} else if (type == 1) {// 忘记密码
					launchRequest(WooRequestFactory.getwangjimima(username
							.getText().toString(), mima.getText().toString(),
							yanzhengma.getText().toString()));
				}
			} else {
				Toast.makeText(this, "密码不一致!", Toast.LENGTH_LONG).show();
			}
			break;
		default:
			break;
		}
	}

	/* 定义一个倒计时的内部类 */
	class MyCount extends CountDownTimer {
		public MyCount(long millisInFuture, long countDownInterval) {
			super(millisInFuture, countDownInterval);
		}

		@Override
		public void onFinish() {
			huoqu.setBackgroundResource(R.drawable.yanzhengmahuang);
			huoqu.setTextColor(Color.WHITE);
			huoqu.setText("获取验证码");
			huoqu.setEnabled(true);
		}

		@Override
		public void onTick(long millisUntilFinished) {
			huoqu.setText("短信已发送(" + millisUntilFinished / 1000 + "秒)");
			// Toast.makeText(RegisteredActivity.this,
			// millisUntilFinished / 1000 + "", Toast.LENGTH_LONG).show();//
			// toast有显示时间延迟
		}
	}

	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		switch (resultCode) { // resultCode为回传的标记，我在B中回传的是RESULT_OK
		case 1:
			setResult(1);
			finish();
		default:
			break;
		}
	}
}
